package com.oc.mba;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.oc.mba.util.CommConsts;
import com.oc.mba.util.DESEncrypt;
import com.oc.mba.util.JsonUtil;
import com.oc.mba.util.MD5;

public class JSONTracer {
	// 开发者ID
	public static String U_ID = "1880577";
	// channelID、APP_KEY
	public static String APP_KEY = "1456771936";
	// 密钥
	public static String SECRET_KEY = "f411ccfa6a5f3ec5";

	public static void main(String[] args) {
//		loadPaper();
		String plaintext = "plainText";
		String desKey = DESEncrypt.createDESkey(100L);
		
		String encryptStr = encrypt(plaintext, desKey);
		String decipherStr = decipher(encryptStr, desKey);
		System.out.println("desKey=" + desKey);
		System.out.println("encryptStr=" + encryptStr);
		System.out.println("decipherStr=" + decipherStr);
		String key = "ad3747a2a3c6ed00";
		String md5 = MD5.encryptHex(key);
		System.out.println(md5);
		
	}

	public static void gfanNotify() {
		StringBuffer request = new StringBuffer();
		request.append("<response>");
		request.append("<order_id>v1381923989557</order_id>");
		request.append("<cost>10</cost>");
		request.append("<appkey>" + APP_KEY + "</appkey>");
		request.append("<create_time>12345678</create_time>");
		request.append("</response>");
		String time = "987654321";
		String sign = MD5.encryptHex(U_ID + time);
		String urlStr = "http://192.168.12.137:8080/ocmba/gfanNofity.do?sign="
				+ sign + "&time=" + time;

		doRequest(request.toString(), urlStr);
	}

	/**
	 * 
	 * @description：发送POST请求
	 * @author xinge21
	 * @time 2013-3-7 下午3:40:30
	 * @param request
	 * @param urlStr
	 * @return String:
	 */
	public static String doRequest(String request, String urlStr) {
		int responseCode = 0;
		StringBuffer buffer = new StringBuffer();
		try {
			URL url = new URL(urlStr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setReadTimeout(60000);
			// User-Agent:值为 packageName=?,appName=?,channelid=?(分隔符为英文逗号)
			conn.setRequestProperty("User-Agent",
					"packageName=com.mappn.sdk.demo.api,appName=GfanSDK_API_Demo,channelID="
							+ APP_KEY);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			Writer writer = new OutputStreamWriter(conn.getOutputStream(),
					"UTF-8");
			writer.write(request);
			writer.close();
			responseCode = conn.getResponseCode();
			System.out.println(responseCode);
			Reader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
			char[] arr = new char[1024];
			int len = 0;
			while ((len = in.read(arr)) != -1) {
				buffer.append(arr, 0, len);
			}
			in.close();
			conn.disconnect();

			System.out.println(buffer.toString());
		} catch (Exception e) {
		} finally {

		}

		return buffer.toString();
	}

	private static void loadPaper() {
		Map<String, Object> req = new HashMap<String, Object>();
		req.put(CommConsts.SIGNID_FLAG, "201");
		req.put("paperID", "8");
		System.out.println(JsonUtil.jsonMTS(req));
	}

	private static void genTest1() {
		Map<String, Object> req = new HashMap<String, Object>();
		req.put(CommConsts.SIGNID_FLAG, 100);
		req.put("androidID", "aid123456");
		req.put("imei", "imei123456");
		System.out.println(JsonUtil.jsonMTS(req));
	}
	
	/**
	 * 
	 * @brief 通过desKey加密数据
	 * 
	 * @req MeetMeMobile-ServerSRS-APPWEB-common-005
	 * @req MeetMeMobile-ServerSRS-APPWEB-common-004
	 * @req MeetMeMobile-ServerSRS-APPWEB-Conf-07
	 * @req MeetMeMobile-ServerSRS-APPWEB-Conf-01
	 * 
	 * 
	 * @param plaintext 明文
	 * @param desKey
	 * @return String
	 */
	public static String encrypt(String plaintext, String desKey) {
		try {
			DESEncrypt des = new DESEncrypt(desKey);
			return des.encrypt(plaintext);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 
	 * @brief 通过key解密数据
	 * 
	 * @req MeetMeMobile-ServerSRS-APPWEB-Dispatch-001
	 * 
	 * @param ciphertext
	 *            密文
	 * @param key
	 * @return String
	 */
	public static String decipher(String ciphertext, String key) {
		try {
			DESEncrypt des = new DESEncrypt(key);
			return des.decrypt(ciphertext);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
}
